package com.shsxt.crm.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shsxt.common.util.Result;
import com.shsxt.common.util.StringUtil;
import com.shsxt.crm.dao.YgSaleChanceDao;
import com.shsxt.crm.model.YgSaleChance;
import com.shsxt.crm.service.IMarketingService;

@Service
public class MarketingServiceImpl implements IMarketingService {

	@Autowired
	private YgSaleChanceDao ygSaleChanceDao;

	@Override
	public Map<String, Object> querySaleChancesByParams(YgSaleChance saleChance) {

		// 分页 pagehelper
		PageHelper.startPage(saleChance.getPage(), saleChance.getRows());
		// 查询 list
		Map<String, Object> params = new HashMap<>();

		if (StringUtil.isNotEmpty(saleChance.getCreateMan())) {
			params.put("createMan", saleChance.getCreateMan());
		}
		if (StringUtil.isNotEmpty(saleChance.getCustomerName())) {
			params.put("customerName", saleChance.getCustomerName());
		}
		if (saleChance.getCreateDate() != null) {
			params.put("createDate", saleChance.getCreateDate());
		}
		if (saleChance.getState() != null) {
			params.put("state", saleChance.getState());
		}
		params.put("isValid", 1);
		List<YgSaleChance> list = ygSaleChanceDao.find(params);

		PageInfo<YgSaleChance> info = new PageInfo<>(list);

		// 返回数据
		Map<String, Object> map = new HashMap<>();
		map.put("total", info.getTotal());
		map.put("rows", info.getList());
		return map;
	}

	@Override
	@Transactional
	public Result delMarketingChance(Integer[] ids) {
		if (ids.length <= 0) {
			return Result.success("未选中记录");
		}
		// 逻辑删除 记录
		YgSaleChance chance = new YgSaleChance();
		chance.setId(ids[0]);
		chance.setIsValid(0);
		// 更新时间
		chance.setUpdateDate(new Date(System.currentTimeMillis()));
		long ste = ygSaleChanceDao.updateSte(chance);
		if (ste == 1) {
			return Result.success("删除成功");
		}
		return Result.success("删除失败");
	}

	/**
	 * 备注： 缺少 记录创建 信息
	 */
	@Override
	@Transactional
	public Result insertMarketingChance(YgSaleChance saleChance) {
		// 设置 创建和 更新时间
		saleChance.setCreateDate(new Date());
		saleChance.setUpdateDate(new Date());
		saleChance.setIsValid(1); // 设置有效
		saleChance.setState(1);
		saleChance.setDevResult(0);// 添加的设置开发中 状态为0
		saleChance.setAssignTime(new Date());
		long ste = ygSaleChanceDao.saveSte(saleChance);
		if (ste == 1) {
			return Result.success("添加成功");
		}
		return Result.fail("添加失败");
	}

	@Override
	@Transactional
	public Result updateMarketingChance(YgSaleChance saleChance) {
		// 设置 创建和 更新时间
		saleChance.setUpdateDate(new Date());
		long ste = ygSaleChanceDao.updateSte(saleChance);
		if (ste == 1) {
			return Result.success("修改该成功");
		}
		return Result.fail("修改失败");
	}

	@Override
	public YgSaleChance queryYgSaleChanceBySid(Integer sid) {
		return ygSaleChanceDao.get(sid);
	}

	@Override
	@Transactional
	public Result updateSaleChanceDevResult(Integer devResult, Integer sid) {

		YgSaleChance ygSaleChance = new YgSaleChance();
		ygSaleChance.setId(sid);
		ygSaleChance.setDevResult(devResult);
		ygSaleChance.setUpdateDate(new Date());

		long ste = ygSaleChanceDao.updateSte(ygSaleChance);
		if (ste == 1) {
			return Result.success("修改该成功");
		}
		return Result.fail("修改失败");
	}

}
